﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.Split;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0203", Name = "年栏记载_规律检证(以内/以外)", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0203:VerfityOneResult
    {

        string _years = string.Empty;

        [Category("设定检查的年"), Description("年出现设定值以外/以外的内容时抽出检证")]
        [EditorAttribute(typeof(PropertyGridRichText), typeof(System.Drawing.Design.UITypeEditor))]
        public String Years
        {
            get { return _years; }
            set { _years = value; }
        }

        [Category("设定检查项以外,以外"), Description("true:以内 false:以外")]
        public bool Contains { get; set; }


        private IEnumerable<string> YearArray
        {
            get
            {

                return _years.Split(',');
            }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            foreach (var rowResult in result)
            {

               
                //公历 -> 和历
                var changeResult = new DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(rowResult);

                if ( EmptyColumnCount(changeResult) == changeResult.Count)
                    continue;

                //for debug
                foreach (var entryResult in changeResult)
                { 
                     if (EmptyResultCount(entryResult).Equals(1))
                         continue;


                     string completeResult = CompleteDate(ChangeResult(entryResult)); ;


            
                   
                    var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat,
                                                                      completeResult);

                    //String entryDateStr = splitEntryResult.GetDate();
                    DateTime? entryDate = splitEntryResult.GetDateTime();


                    //得到年位置
                    if (entryDate != null)
                    {
                        string year = ((DateTime)entryDate).Year.ToString(CultureInfo.InvariantCulture);

                       // System.Console.WriteLine(string.Format("category{0} year:{1}", ChildImageObject.CategoryId, year));


                        if (Contains)
                        {
                            //是否包含年
                            if (Years != null)
                            {
                                bresult = YearArray.Contains(year);
                            }    
                        }
                        else
                        {
                            //是否不包含年
                            if (Years != null)
                            {
                                bresult = !YearArray.Contains(year);
                            }    
                        }
                        

                    }

                    if (bresult)
                        break;
                }


                if (bresult)
                    break;

            } 

            if (bresult)
            {
                System.Console.WriteLine("VerfityRq0203");
            }

            return bresult;   
        }
         
        /// <summary>
        /// 月 日项目有？ 去掉问号
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private string ChangeResult(string result)
        {
            var formatsplt = this.ChildImageObject.CategorySetting.DataFormat.Split('/');

            var resultsplt = result.Split('/');

            for (int i = 0; i < formatsplt.Length; i++)
            {
                var format = formatsplt[i];
                if (format.Equals("gg") ||
                    format.Equals("yy") ||
                    format.Equals("yyyy"))
                {
                    continue;
                }

                if (resultsplt.Length > i && resultsplt[i].Equals("?"))
                {
                    resultsplt[i] = string.Empty;
                }
            }

            return string.Join("/", resultsplt);
        }

    }
}